package com.tmobile.databaseprepare;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import android.content.Context;
import android.text.Html;
import android.util.Log;

import com.tmobile.databaseprepare.database.BookDatabase;

public class HtmlFileReader {
	private static final String TAG = "HtmlFileReader";
	private Context mContext;
	BookDatabase bookDatabase;
	int numberOfChapter = 2;

	public HtmlFileReader(Context ctx) {
		mContext = ctx;
		bookDatabase = BookDatabase.getInstance(ctx);
	}

	public void readFile(String filePath) {
		BufferedReader reader = null;
		Log.d(TAG, "" + filePath);
		try {
			reader = new BufferedReader(new InputStreamReader(mContext
					.getAssets().open(filePath), "UTF-8"));

			// do reading, usually loop until end of file reading
			String mLine = reader.readLine();
			StringBuffer sb = new StringBuffer();
			while (mLine != null) {
				// process line
				sb.append(mLine);
				mLine = reader.readLine();

			}
			insertToDB(sb.toString());
		} catch (IOException e) {
			// log the exception
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public void readAllFile() {
		for (int i = 0; i <= numberOfChapter; i++) {
			Log.i(TAG, ""+i);
			readFile("index_split_00" + i + ".html");
		}

	}

	public void insertToDB(String sb) {
		com.tmobile.databaseprepare.database.Book bookRecord = new com.tmobile.databaseprepare.database.Book();
		bookRecord.setBookNumber(1);
		bookRecord.setChapterTitle("");
		bookRecord.setChapterContent(sb);
		bookDatabase.createRecord(bookRecord);
	}
}